package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.absentactivities.AbsentActivitiesAddDto;
import com.teacher.pojo.dto.absentactivities.AbsentActivitiesGetDto;
import com.teacher.pojo.dto.absentactivities.AbsentActivitiesUpdateDto;
import com.teacher.pojo.entity.AbsentActivities;
import com.teacher.pojo.vo.absentactivities.AbsentActivitiesGetVo;
import com.teacher.service.IAbsentActivitiesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 班级活动缺席 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-08-17
 */
@RestController
@RequestMapping("/pc/absent-activities")
@Api(tags = "班级活动缺席")
public class AbsentActivitiesController {

    @Autowired
    private IAbsentActivitiesService iAbsentActivitiesService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 添加班级活动缺席
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加班级活动缺席 absentActivities_add")
    @PostMapping
    @PreAuthorize("hasAuthority('absentActivities_add')")
    public CommonResult<String> add(@RequestBody @Validated AbsentActivitiesAddDto dto) {
        AbsentActivities absentActivities = new AbsentActivities();
        BeanUtils.copyProperties(dto, absentActivities);
        absentActivities.setTermId(termMapper.getCurrentTerm());
        if (iAbsentActivitiesService.save(absentActivities)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 更新班级活动缺席
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新班级活动缺席 absentActivities_update")
    @PutMapping
    @PreAuthorize("hasAuthority('absentActivities_update')")
    public CommonResult<String> update(@RequestBody @Validated AbsentActivitiesUpdateDto dto) {
        AbsentActivities absentActivities = new AbsentActivities();
        BeanUtils.copyProperties(dto, absentActivities);
        if (iAbsentActivitiesService.updateById(absentActivities)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除班级活动缺席
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除班级活动缺席 absentActivities_delete")
    @PreAuthorize("hasAuthority('absentActivities_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iAbsentActivitiesService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询班级活动缺席
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询班级活动缺席 absentActivities_get")
    @PreAuthorize("hasAuthority('absentActivities_get')")
    public CommonResult<IPage<AbsentActivitiesGetVo>> getPage(AbsentActivitiesGetDto dto) {
        return iAbsentActivitiesService.getPage(dto);
    }


//    @GetMapping("/insert")
//    public CommonResult<String> test() {
//        List<AbsentActivities> absentActivitiesList = new ArrayList<>();
//
//        List<Long> termIds = new ArrayList<>();
//        termIds.add(1419557592744738818L);
//        termIds.add(1419560625998282753L);
//        termIds.add(1419566339911204865L);
//        termIds.add(1423593417044303873L);
//        termIds.add(1423597804110127105L);
//
//        for (int i = 0; i <= 100000; i++) {
//            AbsentActivities activities = new AbsentActivities();
//            activities.setTermId(termIds.get(RandomUtil.randomInt(0, 4)));
//            activities.setStudentKey(new Random(999999999).nextLong() + "");
//            activities.setDoTime(new Date());
//            activities.setTitle(RandomUtil.randomString(20));
//            activities.setRemark(RandomUtil.randomString(20));
//            absentActivitiesList.add(activities);
//        }
//        iAbsentActivitiesService.saveBatch(absentActivitiesList);
//        return CommonResult.success("111");
//    }

}

